﻿$(function () {

    $.views.converters({
        toArray: function (value) {
            return _.toArray(value);
        }
    });

    $.views.converters({
        shortName: function (value) {
            return value.substring(value.lastIndexOf(".") + 1, value.length);
        }
    });

    $.views.helpers({
        getFields: function (object) {
            var key, value,
                fieldsArray = [];
            for (key in object) {
                if (object.hasOwnProperty(key)) {
                    value = object[key];
                    // For each property/field add an object to the array, with key and value
                    fieldsArray.push({
                        key: key,
                        value: value
                    });
                }
            }
            // Return the array, to be rendered using {{for ~fields(object)}}
            return fieldsArray;
        }
    });

    $.views.helpers({
        getProperties: function (object) {
            var key, value,
                fieldsArray = [];
            for (key in object) {
                if (object.hasOwnProperty(key)) {
                    value = object[key];

                    if (value.memberType === "property" || value.memberType === "collection") {

                        // For each property/field add an object to the array, with key and value
                        fieldsArray.push({
                            key: key,
                            value: value
                        });
                    }
                }
            }
            // Return the array, to be rendered using {{for ~fields(object)}}
            return fieldsArray;
        }
    });

    $.views.helpers({
        getActions: function (object) {
            var key, value,
                fieldsArray = [];
            for (key in object) {
                if (object.hasOwnProperty(key)) {
                    value = object[key];

                    if (value.memberType === "action") {

                        // For each property/field add an object to the array, with key and value
                        fieldsArray.push({
                            key: key,
                            value: value
                        });
                    }
                }
            }
            // Return the array, to be rendered using {{for ~fields(object)}}
            return fieldsArray;
        }
    });

    $.views.helpers({
        getFindActions: function (object, returnType) {
            var key, value,
                fieldsArray = [];
            for (key in object) {
                if (object.hasOwnProperty(key)) {
                    value = object[key];

                    if (value.extensions.returnType === returnType) {

                        // For each property/field add an object to the array, with key and value
                        fieldsArray.push({
                            key: key,
                            value: value
                        });
                    }
                }
            }
            // Return the array, to be rendered using {{for ~fields(object)}}
            return fieldsArray;
        }
    });

    $.views.helpers({
        isFindAction: function (object, returnType) {
            if (object.extensions.returnType === returnType || object.extensions.elementType === returnType) {

                if (object.extensions.hasParams) {

                    return _.all(object.parameters, function (i) {

                        return i.extensions.returnType === "integer" ||
                            i.extensions.returnType === "string" ||
                                i.extensions.returnType === "number" ||
                                    i.extensions.returnType === "boolean";
                    });
                }

                return true;
            }
            return false;
        }
    });

    $.views.helpers({
        hasFinder: function(object) {
            return !object.disabledReason;
        }
    });



    $.views.helpers({
        isScalar: function(typeString) {
            return (typeString === "integer" || typeString === "number" || typeString === "string" || typeString === "boolean");
        }
    });

    $.views.helpers({
        isCollection: function(typeString) {
            return (typeString === "list");
        }        
    });

    $.views.helpers({
        isReference: function(typeString) {
            return !$.views.helpers.isScalar(typeString) && !$.views.helpers.isCollection(typeString);
        }
    });


    $.templates("serviceMenu",
        "<div class='nof-menuname'>{{:title}}</div>" +
            "<div class='nof-menuitems'>" +
                "{{for ~getFields(members) }}" +
                    "{{if value.disabledReason }}" +
                        "<div class='nof-action' title='{{:value.disabledReason}}'>{{:value.extensions.friendlyName}}</div>" +
                            "{{else}}" +
                                "<form class='nof-action' id='{{:value.id}}'><div><button>{{:value.extensions.friendlyName}}</button></div></form>" +
                                    "{{/if}}" +
                                        "{{/for}}");

    $.templates("subMenu",
        "<div class='nof-menuname'>{{:extensions.friendlyName}}</div>" +
            "<div class='nof-submenuitems'></div>");

    $.templates("subMenuItem",
        "{{if ~isFindAction(#data, ~returnType) }}" +
            "<form class='nof-action' id='{{:id}}'><div><button>{{:extensions.friendlyName}}</button></div></form>" +
                "{{/if}}");

    $.templates("dialog",
        "<div class='nof-object><a></a></div>" +
            "<div class='nof-actionname'>{{:extensions.friendlyName}}</div>" +
                "<form class='nof-dialog'>" +
                    "<div class='nof-parameterlist'>" +
                        "{{for parameters}}" +
                            "<div class='nof-parameter' id={{:name}}  >" +
                                "<label>{{:extensions.friendlyName}}:</label>" +
                                    "{{if ~isScalar(extensions.returnType)}}" +
                                        "<div class='nof-value'><input></input>" +
                                            "{{else}}" +
                                                "<div class='nof-object' data-type='{{:extensions.returnType}}' >" +
                                                    "{{/if}}" +
                                                        "<span class='nof-mandatory-field-indicator'>{{if extensions.optional === false}}'*'{{/if}}</span>" +
                                                            "</div>" +
                                                                "</div>" +
                                                                    "{{/for}}" +
                                                                        "</div>" +
                                                                            "<button class='nof-ok'>OK</button>" +
                                                                                "</form>" +
                                                                                    "<form class='nof-action'>" +
                                                                                        "<div><button class='nof-cancel'>Cancel</button></div>" +
                                                                                            "</form>");

    $.templates("findDialog",
        "<div class='nof-actionname'>{{:extensions.friendlyName}}</div>" +
            "<div class='nof-parameterlist'>" +
                "{{for parameters}}" +
                    "<div class='nof-parameter' id={{:name}}  >" +
                        "<label>{{:extensions.friendlyName}}:</label>" +
                            "{{if ~isScalar(extensions.returnType)}}" +
                                "<div class='nof-value'><input></input>" +
                                    "{{else}}" +
                                        "<div class='nof-object'>" +
                                            "{{/if}}" +
                                                "<span class='nof-mandatory-field-indicator'>{{if extensions.optional === false}}'*'{{/if}}</span>" +
                                                    "</div>" +
                                                        "</div>" +
                                                            "{{/for}}" +
                                                                "</div>" +
                                                                    "<button class='nof-ok'>OK</button>" +
                                                                        "<form class='nof-action'>" +
                                                                            "<div><button class='nof-cancel'>Cancel</button></div>" +
                                                                                "</form>");

    $.templates("errorTemplate",
        "<h2>{{:message}}</h2><p>{{:stackTrace}}</p>");

    $.templates("object",
        "<div class='nof-object'><img src='/Content/Default.png'' alt=''>{{:title}}</div>");

    // todo extract commonality for service menu into common template 
    $.templates("menu",
        "<div id='{{shortName:domainType}}-Actions'  class='nof-menu'>" +
            "<div class='nof-menuname'>Actions</div>" +
                "<div class='nof-menuitems'>" +
                    "{{for ~getActions(members) }}" +
                        "<form class='nof-action' id='{{:value.id}}'><div><button>{{:value.extensions.friendlyName}}</button></div></form>" +
                            "{{/for}}" +
                                "</div>" +
                                    "</div>");


    $.templates("viewProperty",
        "{{if !~isEdit && ~isScalar(value.extensions.returnType) tmpl='viewScalarProperty'/}}" +
        "{{if ~isEdit && ~isScalar(value.extensions.returnType) tmpl='editScalarProperty'/}}" +
        "{{if !~isEdit && ~isReference(value.extensions.returnType) tmpl='viewReferenceProperty'/}}" +
        "{{if ~isEdit && ~isReference(value.extensions.returnType) tmpl='editReferenceProperty'/}}" +
        "{{if !~isEdit && ~isCollection(value.extensions.returnType) tmpl='viewCollectionProperty'/}}" +
        "{{if ~isEdit && ~isCollection(value.extensions.returnType) tmpl='editCollectionProperty'/}}"
    );


    $.templates("viewScalarProperty",
        "<div class='nof-property'>" +
            "<label>{{:value.extensions.friendlyName}}</label>" +
                "<div class='nof-value'>{{:value.value}}</div></div>");

    $.templates("editScalarProperty",
        "<div class='nof-property'>" +
            "<label>{{:value.extensions.friendlyName}}</label>" +
                "<div class='nof-value'><input id='{{:value.id}}' value='{{:value.value}}'/></div></div>");

    $.templates("viewReferenceProperty",
        "<div class='nof-property'>" +
            "<label>{{:value.extensions.friendlyName}}</label>" +
                "<div class='nof-object' data-type='{{:value.extensions.returnType}}'>" +
                    "{{if value.value !== null }}" +
                        "<img src='/Content/Default.png' alt=''>" +
                            "<a href='{{:value.value.href}}'>{{:value.value.title}}</a>" +
                                "<form><div><button class='nof-minimize' style='display: none;'>Collapse</button><button class='nof-maximize' style='display: inline';>Expand</button></div></form>" +
                                    "{{/if}}" +
                                        "</div></div>");

    $.templates("editReferenceProperty",
        "<div class='nof-property'>" +
            "<label>{{:value.extensions.friendlyName}}</label>" +
                "<div class='nof-object' data-type='{{:value.extensions.returnType}}' {{if ~hasFinder(value)}}data-hasFinder='true'{{/if}} >" +
                    "{{if value.value !== null }}" +
                        "<img src='/Content/Default.png' alt=''>" +
                            "<a href='{{:value.value.href}}'>{{:value.value.title}}</a>" +
                                "{{/if}}" +
                                    "</div></div>");

    $.templates("viewCollectionProperty",
        "<div class='nof-property'>" +
            "<label>{{:value.extensions.friendlyName}}</label>" +
                "<div class='nof-collection-summary'>" +
                    "<div class='nof-object'>{{if value.size === 0}}No{{else}}{{:value.size}}{{/if}} {{:value.extensions.pluralName}}</div>" +
                        "</div></div>");

    $.templates("property",
        "<div class='nof-propertylist'>" +
            "{{for ~getProperties(members)  tmpl='viewProperty'/}}" +
                "</div>");

    $.templates("table",
        "<div class='nof-object'>Viewing {{: ~count}} of {{: ~count}}</div>" +
            "<form class='nof-action'><div><div class='nof-collectiontable'>" +
                "<table><tbody><tr></tr></tbody></table>" +
                    "</div></div></form>");

    $.templates("row",
        "<tr>" +
            "<td><div class='nof-object'><img/><a  class='selfLink' href='{{: ~selfLink}}'>{{:title}}</a></div></td>" +
                "{{for ~getProperties(members)}}" +
                    "<td><div class='nof-value'>{{:value.value}}</div></td>" +
                        "{{/for}}" +
                            "</tr>");

    $.templates("header",
        "<th></th>" +
            "{{for ~getProperties(members)}}" +
                "<th>{{:key}}</th>" +
                    "{{/for}}");


    $.templates("inlineList",
        "<table>" +
        "<tbody>" +

        "{{for #data}}" +
        "<tr><td><div>{{:title}}<a href='{{:href}}'>Details</a><button>Select</button></div></td></tr>" +
        "{{/for}}" +

        "</tbody>" +
        "</table>");
});



































































































































































